package com.jackie.setting

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class NameAdapter(val context: Context, var list: List<String>, var selectedName: String?) :
    BaseAdapter<String, NameAdapter.Companion.NameViewHold>(list) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NameViewHold {
        val layout = LayoutInflater.from(context).inflate(R.layout.layout_name, parent, false)
        return NameViewHold(layout)
    }

    override fun onBindViewHolder(holder: NameViewHold, position: Int) {
        super.onBindViewHolder(holder, position)
        val name = data[position]
        holder.itemView.isSelected = name == selectedName
        holder.setEntry(name)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    companion object {
        class NameViewHold(itemView: View) : RecyclerView.ViewHolder(itemView) {
            private val tvName: TextView by lazy {
                itemView.findViewById(R.id.tv_item_name)
            }

            fun setEntry(name: String){
                tvName.text = name
            }
        }
    }
}